package com.hall.items.ctrl;

import com.hall.items.aggr.*;
import com.hall.items.ctrl.vo.MyPlayerWeaponPackVO;
import com.hall.items.ctrl.vo.MyPlayersStorageItemVO;
import com.hall.items.msg.MsgSender;
import com.hall.items.service.ItemsService;
import com.sharecommon.view.CommonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/items")
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @Autowired
    private MsgSender msgSender;

    @RequestMapping("/playercurrentpack")
    @ResponseBody
    public CommonVO playercurrentpack(String playerId) {
        MyPlayerWeaponPack pack = itemsService
                .queryCurrentPlayerWeaponPack(Long.valueOf(playerId));
        List<Long> weaponIds = pack.getEquipedWeaponIds();
        List<MyWeaponLevel> weaponLevels = itemsService
                .queryPlayerWeaponLevels(weaponIds);
        MyWeaponPackCombatValue packCombatValue = itemsService
                .queryPackCombatValue((long) pack.getId());
        return CommonVO.success(new MyPlayerWeaponPackVO(pack, packCombatValue,
                weaponLevels));
    }

    @RequestMapping("/weaponitems")
    @ResponseBody
    public CommonVO weaponitems(String playerId) {
        List<MyWeapon> items = itemsService.queryPlayerWeapons(Long
                .valueOf(playerId));
        List<MyWeaponEquip> weaponEquips = itemsService
                .queryPlayerWeaponEquips(Long.valueOf(playerId));
        List<MyWeaponLevel> weaponLevels = itemsService
                .queryPlayerWeaponLevels(Long.valueOf(playerId));
        Map weaponEquipsMap = new HashMap();
        for (MyWeaponEquip myWeaponEquip : weaponEquips) {
            weaponEquipsMap.put(myWeaponEquip.getWeapon().getId(),
                    myWeaponEquip);
        }
        Map weaponLevelsMap = new HashMap();
        for (MyWeaponLevel myWeaponLevel : weaponLevels) {
            weaponLevelsMap.put(myWeaponLevel.getWeapon().getId(),
                    myWeaponLevel);
        }
        List<MyPlayersStorageItemVO> vos = new ArrayList<>();
        for (MyWeapon item : items) {
            vos.add(new MyPlayersStorageItemVO(item,
                    (MyWeaponEquip) weaponEquipsMap.get(item.getId()),
                    (MyWeaponLevel) weaponLevelsMap.get(item.getId())));
        }
        return CommonVO.success(vos);
    }

    @RequestMapping("/equipweapon")
    @ResponseBody
    public CommonVO equipweapon(String playerId, long itemId) {
        itemsService.equipWeapon(Long.valueOf(playerId), itemId);
        msgSender.sendProcess(false);
        try {
            Thread.sleep(300);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return CommonVO.success();
    }

    @RequestMapping("/unloadweapon")
    @ResponseBody
    public CommonVO unloadweapon(String playerId, long itemId) {
        itemsService.unloadWeapon(Long.valueOf(playerId), itemId);
        msgSender.sendProcess(false);
        return CommonVO.success();
    }

}
